package com.mp.approvalreq.servlet.comment;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.Query;
import com.mp.approvalreq.service.ApprovalCommentServicePrx;
import com.mp.approvalreq.servlet.AbstractServlet;
import com.mp.user.entity.User;
import com.plusmoney.util.Utils;
import com.qq.tars.client.util.ServantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 审批获取可以@的用户人群,返回的User中是userId(测试ok)
 */
@WebServlet("/getCanAtUserList")
public class GetCanAtUserList extends AbstractServlet {
    private static final Logger logger = LoggerFactory.getLogger(GetCanAtUserList.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        try {
            User user = getReqUser(req);
            String reqBody = getContent(req);
            Query query = JSONObject.parseObject(reqBody, Query.class);

            ApprovalCommentServicePrx commentServicePrx = ServantUtils.getObject(ApprovalCommentServicePrx.class);
            List<Integer> contactIds = commentServicePrx.getCanAtUserList(query);

            if (Utils.isNotEmpty(contactIds)) {
                contactIds.removeAll(user.getMultiContactIdList());
            }

            returnResult(resp, contactIds);
        } catch (Throwable e) {
            onThrowable(req, resp, e);
        }
    }

    private void returnResult(HttpServletResponse resp, Object object) throws Throwable {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("respCode", "000");
        jsonObject.put("respDesc", "返回信息成功");
        jsonObject.put("item", object);
        logger.info("艾特用户列表返回:{}", jsonObject.toJSONString());

        resp.setHeader("Content-Type", "application/json;charset = utf-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(jsonObject.toJSONString());
        resp.flushBuffer();
    }


}
